#  -*-  coding: utf-8 -*-

import os
import re
import time
import requests
from datetime import datetime, timedelta
from bs4 import BeautifulSoup

from text_to_speech import TextToSpeech
from weather_info import WeatherInfo


class Reporter():
    def __init__(self):
        self.cur_path = os.path.split(os.path.realpath(__file__))[0]
        #print('cur_path=%s' % self.cur_path)
        self.output_path = os.path.abspath(os.path.join(self.cur_path, os.pardir, os.pardir, 'output'))
        if not os.path.exists(self.output_path):
            os.makedirs(self.output_path)

        #print('output_path=%s' % self.output_path)

        self.headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 '
                          '(KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36'
        }

        self.weather = WeatherInfo()
        self.tts = TextToSpeech(self.output_path)

    def numtoZh(self, num):
        num_dict = {1: '一', 2: '二', 3: '三', 4: '四', 5: '五', 6: '六', 7: '七',
                    8: '八', 9: '九', 0: '零'}
        num = int(num)
        if 100 <= num < 1000:
            b_num = num // 100
            s_num = (num-b_num*100) // 10
            g_num = (num-b_num*100) % 10
            if g_num == 0 and s_num == 0:
                num = '%s百' % (num_dict[b_num])
            elif s_num == 0:
                num = '%s百%s%s' % (num_dict[b_num], num_dict.get(s_num, ''), num_dict.get(g_num, ''))
            elif g_num == 0:
                num = '%s百%s十' % (num_dict[b_num], num_dict.get(s_num, ''))
            else:
                num = '%s百%s十%s' % (num_dict[b_num], num_dict.get(s_num, ''), num_dict.get(g_num, ''))
        elif 10 <= num < 100:
            s_num = num // 10
            g_num = (num-s_num*10) % 10
            if g_num == 0:
                g_num = ''
            num = '%s十%s' % (num_dict[s_num], num_dict.get(g_num, ''))
        elif 0 <= num < 10:
            g_num = num
            num = '%s' % (num_dict[g_num])
        elif -10 < num < 0:
            g_num = -num
            num = '零下%s' % (num_dict[g_num])
        elif -100 < num <= -10:
            num = -num
            s_num = num // 10
            g_num = (num-s_num*10) % 10
            if g_num == 0:
                g_num = ''
            num = '零下%s十%s' % (num_dict[s_num], num_dict.get(g_num, ''))
        return num


    def get_seconds(self, h='07', m='30', s='00'):
        """获取当前时间与程序启动时间间隔秒数"""

        # 设置程序启动的时分秒
        time_pre = '%s:%s:%s' % (h, m, s)
        # 获取当前时间
        time1 = datetime.now()
        # 获取程序今天启动的时间的字符串格式
        time2 = time1.date().strftime('%Y-%m-%d') + ' ' + time_pre
        # 转换为datetime格式
        time2 = datetime.strptime(time2, '%Y-%m-%d %H:%M:%S')
        # 判断当前时间是否晚于程序今天启动时间，若晚于则程序启动时间增加一天
        if time1 > time2:
            time2 = time2 + timedelta(days=1)

        return time.mktime(time2.timetuple()) - time.mktime(time1.timetuple())


    def broadcast(self):
        print('prepare broadcast')
        #s = get_seconds()
        #time.sleep(s)
        # 获取需要转换语音的文字

        weather_text = self.weather.getWeather()
        print('weather_text=%s' % weather_text)

        mp3_filename = self.tts.getSpeech(weather_text)
        print('mp3_filename=%s' % mp3_filename)

        # 获取音乐文件绝对地址
        mp3_fullpath = os.path.join(os.path.dirname(__file__), mp3_filename)

        # 先播放一首音乐做闹钟
        os.system('mplayer %s' % mp3_fullpath)
        # 播报语音天气
        mp3path1 = os.path.join(os.path.dirname(__file__), mp3_fullpath)
        os.system('mplayer %s' % mp3_fullpath)
        #os.remove(mp3_fullpath)


if __name__ == '__main__':
    reporter = Reporter()
    reporter.broadcast()

